package rdd.operate;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class Spark43_Operate_flatMap {
    public static void main(String[] args) {
        final SparkConf conf = new SparkConf();
        conf.setMaster("local");
        conf.setAppName("spark");
        final JavaSparkContext jsc = new JavaSparkContext(conf);
        final JavaRDD<String> rdd = jsc.textFile("data/test2.txt");
        final JavaRDD<Integer> rdd1 = rdd.flatMap(new FlatMapFunction<String, Integer>() {
            @Override
            public Iterator<Integer> call(String line) throws Exception {
                Iterator iterator =Arrays.asList(line.split(" ")).iterator();           //String没有迭代器，必须把切割出来的数据转为集合List
                return iterator;
            }
        });
        rdd1.collect().forEach(System.out::println);
        jsc.close();
    }
}
